﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Meeting.Servers
{
    public class MeetingSummaryServers
    {
        private readonly IRepository<meeting> meetingRepository;

        public MeetingSummaryServers()
        {
            meetingRepository = new DapperRepository<meeting>();

        }

        public List<meeting> GetData(string m_name,string m_proposer,string m_start, string m_end,string m_room, string summary )
        {
            var datas = meetingRepository.GetAllEnumerable();

            if (!string.IsNullOrEmpty(m_name))
            {
                datas = datas.Where(m => m.M_NAME.Contains(m_name));
            }
            if (!string.IsNullOrEmpty(m_proposer))
            {
                datas = datas.Where(m => m.M_PROPOSER == m_proposer);
            }
            if (!string.IsNullOrEmpty(m_start))
            {
                datas = datas.Where(m => m.M_START == DateTime.Parse(m_start));
            }
            if (!string.IsNullOrEmpty(m_end))
            {
                datas = datas.Where(m => m.M_END == DateTime.Parse(m_end));
            }
            if (!string.IsNullOrEmpty(m_room))
            {
                datas = datas.Where(m => m.M_ROOM == int.Parse(m_room));
            }
            if (!string.IsNullOrEmpty(summary))
            {
                datas = datas.Where(m => m.SUMMARY.Contains(summary));
            }
            return datas.ToList();
        }

        public meeting GetModelById(string id)
        {
            return meetingRepository.GetById(id);
        }


        internal void Add(meeting model)
        {
            meetingRepository.Insert(model);
        }

        internal void Update(meeting model)
        {
            meetingRepository.Update(model);
        }

        internal void Del(string id)
        {
            meetingRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetM_ROOM(int flag)
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meetingRepository.SqlWithParamsDynamic("select MR_ID Value,MR_NAME Text from [dbo].[meeting_room]");
            tables.Clear();
            if (flag == 1)
            {
                tables.Add(new SelectListItem { Value = "", Text = "" });
            }

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetM_STATUS()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meetingRepository.SqlWithParamsDynamic("select Code_name Text,Code_no Value from [sys_code] where PARENT_NO = (select CODE_ID from [dbo].[sys_code] where code_no ='ConferenceStatus')");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetM_MANAGER()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meetingRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetCYCLE_NO()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meetingRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetSUMMARY_STATUS()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = meetingRepository.SqlWithParamsDynamic("select Code_name Text,Code_no Value from [sys_code] where PARENT_NO = (select code_no from [dbo].[sys_code] where code_no ='MinutesOfMeeting')");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


    }
}